package boot

import (
	"gino/app/service"
	"gino/global"
	"gino/pkg/glog"
	"gino/pkg/utils"
	"github.com/robfig/cron"
	"log"
	"time"
)

type Job struct{}

func (j Job) Run() {
	// todo
	log.Printf("Job R 当前时间：%v", time.Now().Format("2006-01-02 15:04:05"))
}

// Cron 定时任务
// https://www.bejson.com/othertools/cron  Cron表达式生成器
func Cron() {
	c := cron.New()

	if global.Config.Cron.Test != "" {
		err := c.AddJob(global.Config.Cron.Test, Job{})
		if err != nil {
			log.Printf("定时任务 Test Error：%s\n", err.Error())
			return
		}
	}

	// 启动计划任务
	c.Start()
}

// AcquireCron 分布式cron
func AcquireCron() (*utils.RedisLock, bool) {
	rl, ok := service.Cron.AcquireCron()
	if ok {
		glog.Info("cron started")
		Cron()
	}

	return rl, ok
}
